<template>
  <view class="profile-container">
    <!-- 用户信息卡片 -->
    <view class="user-card">
      <view class="user-info">
        <view class="avatar-wrapper">
          <image class="avatar" :src="userInfo ? userInfo.avatarUrl : '/static/images/default-avatar.svg'"
            mode="aspectFill">
          </image>
        </view>
        <view class="user-details">
          <text class="username">{{ userInfo ? userInfo.nickname : '加载中...' }}</text>
          <view class="points-info" v-if="userInfo" @click.stop="navigateTo('/pages/points/detail')">
            <text class="points-value">{{ userInfo.points || 0 }}</text>
            <text class="points-label">积分</text>
            <text class="points-arrow iconfont icon-arrow"></text>
          </view>
        </view>
      </view>
    </view>

    <!-- 功能模块 -->
    <view class="function-group">
      <view class="group-title">回收服务</view>
      <view class="function-list">
        <view class="function-item" @click="navigateTo('/pages/recycle/order-detail')">
          <text class="function-icon iconfont icon-recycle"></text>
          <text class="function-text">回收订单</text>
          <text class="function-arrow iconfont icon-arrow"></text>
        </view>
        <view class="function-item" @click="navigateTo('/pages/recycle/nearby')">
          <text class="function-icon iconfont icon-dingwei"></text>
          <text class="function-text">附近回收点</text>
          <text class="function-arrow iconfont icon-arrow"></text>
        </view>
      </view>
    </view>

    <view class="function-group">
      <view class="group-title">个人设置</view>
      <view class="function-list">
        <view class="function-item" @click="navigateTo('/pages/address/manage')">
          <text class="function-icon iconfont icon-address-book"></text>
          <text class="function-text">地址管理</text>
          <text class="function-arrow iconfont icon-arrow"></text>
        </view>
        <view class="function-item" @click="openFeedback()">
          <text class="function-icon iconfont icon-yijianfankui"></text>
          <text class="function-text">意见反馈</text>
          <text class="function-arrow iconfont icon-arrow"></text>
        </view>
        <view class="function-item" @click="navigateTo('/pages/settings/about')">
          <text class="function-icon iconfont icon-l-about"></text>
          <text class="function-text">关于我们</text>
          <text class="function-arrow iconfont icon-arrow"></text>
        </view>
      </view>
    </view>

    <!-- 联系客服 -->
    <view class="customer-service">
      <button class="service-button" open-type="contact">联系客服</button>
    </view>

    <!-- 退出登录按钮 -->
    <view class="logout-section">
      <button class="logout-button" @click="logoutUser">退出登录</button>
    </view>
  </view>
</template>

<script>
import authMixin from '../../mixins/auth-mixin.js';
import { isLoggedIn, getUserInfo, logout } from '../../utils/auth.js';

export default {
  mixins: [authMixin],

  data() {
    return {
      userInfo: null,
      // 设置整个页面需要登录权限
      authRequired: true
    };
  },

  onLoad() {
    // 页面加载时检查登录状态
    this.updateUserInfo();
  },

  onShow() {
    // 页面显示时更新用户信息
    this.updateUserInfo();
  },

  // 登录成功回调
  onLoginSuccess() {
    this.updateUserInfo();
  },

  methods: {
    // 获取用户信息
    updateUserInfo() {
      this.isUserLoggedIn = isLoggedIn();
      this.userInfo = getUserInfo();
    },

    // 页面导航
    navigateTo(url) {
      // 因为整个页面已经需要登录，所以这里可以直接跳转
      uni.navigateTo({ url });
    },

    // 打开意见反馈
    openFeedback() {
      uni.navigateTo({
        url: '/pages/feedback/index'
      });
    },

    // 登录
    login() {
      if (!this.userInfo || !this.isUserLoggedIn) {
        uni.navigateTo({
          url: '/pages/login/index'
        });
      }
    },

    // 退出登录
    logoutUser() {
      if (this.isUserLoggedIn) {
        uni.showModal({
          title: '提示',
          content: '确定要退出登录吗？',
          success: (res) => {
            if (res.confirm) {
              logout();
              this.userInfo = null;
              this.isUserLoggedIn = false;

              // 退出登录后，跳转到首页
              uni.showToast({
                title: '已退出登录',
                icon: 'success'
              });

              setTimeout(() => {
                uni.switchTab({
                  url: '/pages/index/index'
                });
              }, 1500);
            }
          }
        });
      }
    }
  }
};
</script>

<style lang="scss">
@import '../../styles/global.scss';

.profile-container {
  background-color: $bg-color;
  min-height: 100vh;
  padding-bottom: 40rpx;
}

/* 用户信息卡片 */
.user-card {
  background-color: $primary-green;
  padding: 40rpx 30rpx;
  color: $text-white;
  margin-bottom: 20rpx;

  .user-info {
    display: flex;
    align-items: center;

    .avatar-wrapper {
      width: 120rpx;
      height: 120rpx;
      border-radius: 50%;
      overflow: hidden;
      background-color: rgba(255, 255, 255, 0.3);
      margin-right: 30rpx;
      border: 4rpx solid rgba(255, 255, 255, 0.4);

      .avatar {
        width: 100%;
        height: 100%;
      }
    }

    .user-details {
      flex: 1;

      .username {
        font-size: 36rpx;
        font-weight: bold;
        margin-bottom: 10rpx;
      }

      .points-info {
        display: flex;
        align-items: center;
        background-color: rgba(255, 255, 255, 0.2);
        padding: 8rpx 20rpx;
        border-radius: 30rpx;
        display: inline-flex;

        .points-value {
          font-size: 28rpx;
          font-weight: bold;
          margin-right: 8rpx;
        }

        .points-label {
          font-size: 24rpx;
        }

        .points-arrow {
          font-size: 24rpx;
          margin-left: 10rpx;
        }
      }
    }
  }
}

/* 功能模块 */
.function-group {
  margin: 20rpx;
  margin-bottom: 30rpx;

  .group-title {
    font-size: 30rpx;
    font-weight: bold;
    color: $text-black;
    margin-bottom: 20rpx;
    padding-left: 20rpx;
    position: relative;

    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 6rpx;
      height: 28rpx;
      background-color: $primary-green;
      border-radius: 3rpx;
    }
  }

  .function-list {
    background-color: $card-bg;
    border-radius: 16rpx;
    overflow: hidden;
    box-shadow: $card-shadow;
  }

  .function-item {
    display: flex;
    align-items: center;
    padding: 30rpx;
    border-bottom: 1rpx solid $border-light;
    position: relative;

    &:last-child {
      border-bottom: none;
    }

    .function-icon {
      font-size: 40rpx;
      color: $primary-green;
      margin-right: 20rpx;
    }

    .function-text {
      flex: 1;
      font-size: 28rpx;
      color: $text-black;
    }

    .function-arrow {
      font-size: 28rpx;
      color: $text-light-gray;
    }
  }
}

/* 客服按钮 */
.customer-service {
  margin: 60rpx 20rpx 20rpx;

  .service-button {
    background-color: $card-bg;
    color: $text-gray;
    font-size: 28rpx;
    padding: 20rpx 0;
    border-radius: 40rpx;
    text-align: center;
    border: 1rpx solid $border-color;
    width: 100%;
    box-shadow: $card-shadow;

    &::after {
      border: none;
    }
  }
}

/* 退出登录按钮 */
.logout-section {
  margin: 20rpx;

  .logout-button {
    background-color: #fff;
    color: #ff6b6b;
    font-size: 28rpx;
    padding: 20rpx 0;
    border-radius: 40rpx;
    text-align: center;
    border: 1rpx solid #ff6b6b;
    width: 100%;

    &::after {
      border: none;
    }

    &:active {
      background-color: #ffe6e6;
    }
  }
}
</style>